package com.bbchond.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import com.bbchond.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bbchond.service.UserService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Controller
public class UserController {

	@Autowired
	private UserService userService;
	
	@ResponseBody
	@RequestMapping("/getUser")
	public List<User> getUser(){
		return userService.getUsers();
	}

	@ResponseBody
	@RequestMapping("/getBBSTopics")
	public List<BBSTopic> getBBSTopics(){
		return userService.getBBSTopics();
	}

	@ResponseBody
	@RequestMapping("/getUserPunch")
	public UserPunch getUserPunch(@RequestParam("user_account") String userAccount){
		return userService.getUserPunch(userAccount);
	}

	@ResponseBody
	@RequestMapping("/register")
	public Verify register(User user, MultipartFile file, HttpServletRequest request) throws IOException {
		String filePath = request.getSession().getServletContext().getRealPath("/upload");
		System.out.println(filePath);
		String newFileName = fileOperate(file, filePath);
		user.setUser_image("http://www.bbchond.cn/SSMDemo/upload/" + newFileName);
		int result = userService.UserRegister(user);
		Verify verify = new Verify();
		if (result > 0) {
			verify.setStatus("true");
		} else {
			verify.setStatus("false");
		}
		return verify;
	}

	private String fileOperate(MultipartFile file, String filePath) {
		String originalFileName = file.getOriginalFilename();//获取原始图片的扩展名
		System.out.println("图片原始名称："+originalFileName);
		String newFileName = UUID.randomUUID()+originalFileName;  //新的文件名称
		System.out.println("新的文件名称："+newFileName);
		File targetFile = new File(filePath,newFileName); //创建新文件
		try {
			file.transferTo(targetFile); //把本地文件上传到文件位置 , transferTo()是springmvc封装的方法，用于图片上传时，把内存中图片写入磁盘
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
		}
		return newFileName;
	}

	@ResponseBody
	@RequestMapping("/replyInfo")
	public List<BBSReply> replyInfo(@RequestParam("bbs_id") int id) {
		return userService.getBBSReply(id);
	}

	@ResponseBody
	@RequestMapping("/replyPost")
	public Verify replyPost(BBSReply bbsReply) {
		int result = userService.ReplyPost(bbsReply);
		Verify verify = new Verify();
		if (result > 0) {
			verify.setStatus("true");
		} else {
			verify.setStatus("false");
		}
		return verify;
	}

	@ResponseBody
	@RequestMapping("/getUserName")
	public User getUserName(@RequestParam("user_account") String userAccount) {
		return userService.getUserName(userAccount);
	}

	@ResponseBody
	@RequestMapping("/BBSPost")
	public Verify BBSPost(BBSTopic bbsTopic) {
		int result = userService.BBSPost(bbsTopic);
		Verify verify = new Verify();
		if (result > 0) {
			verify.setStatus("true");
		} else {
			verify.setStatus("false");
		}
		return verify;
	}

	@ResponseBody
	@RequestMapping("/getCharge")
	public List<BBSTopic> getCharge(){
		return userService.getCharge();
	}
	@ResponseBody
	@RequestMapping("/getCardInfo")
	public Card getCardInfo(@RequestParam("user_account") String userAccount) {
		return userService.getCardInfo(userAccount);
	}

	@ResponseBody
	@RequestMapping("/getVideoInfo")
	public List<VideoInfo> getVideoInfo() {
		return userService.getVideoInfos();
	}

	@ResponseBody
	@RequestMapping("/getGoodVideo")
	public List<VideoInfo> getGoodVideoInfo() {
		return userService.getGoodVideoInfo();
	}


	@ResponseBody
	@RequestMapping("/videoReplyInfo")
	public List<VideoReply> videoReplyInfo(@RequestParam("video_id") int id) {
		return userService.getReplyInfo(id);
	}

	@ResponseBody
	@RequestMapping("/userLogin")
	public Verify userLogin(ClassUser classUser) {
		Verify ver = new Verify();
		ClassUser u = userService.userVerify(classUser);
		if (u != null) {
			ver.setStatus("true");
		} else {
			ver.setStatus("false");
		}
		return ver;
	}

	@ResponseBody
	@RequestMapping("/getUserInfo")
	public ClassUser getUserInfo(@RequestParam("user_account") String userAccount) {
		return userService.userInfo(userAccount);
	}

	@ResponseBody
	@RequestMapping("/getTeacher")
	public Teacher getTeacher(@RequestParam("teacher_name") String teacherName) {
		return userService.teacherInfo(teacherName);
	}

	@ResponseBody
	@RequestMapping("/getTeacherInfo")
	public List<Teacher> getTeacherInfo() {
		return userService.getAllTeacherInfo();
	}

	@ResponseBody
	@RequestMapping("/videoReplyPost")
	public Verify VideoReplyPost(VideoReply videoReply) {
		int result = userService.videoReplyPost(videoReply);
		Verify verify = new Verify();
		if (result > 0) {
			verify.setStatus("true");
		} else {
			verify.setStatus("false");
		}
		return verify;
	}

	@ResponseBody
	@RequestMapping("/repairInfoPost")
	public Verify repairInfoPost(RepairInfo repairInfo) {
		int result = userService.repairPost(repairInfo);
		Verify verify = new Verify();
		if (result > 0) {
			verify.setStatus("true");
		} else {
			verify.setStatus("false");
		}
		return verify;
	}

	@ResponseBody
	@RequestMapping("/classRegister")
	public Verify classRegister(ClassUser classUser) {
		int result = userService.classRegister(classUser);
		Verify verify = new Verify();
		if (result > 0) {
			verify.setStatus("true");
		} else {
			verify.setStatus("false");
		}
		return verify;
	}

	@ResponseBody
	@RequestMapping("/updateClassUserInfo")
	public Verify updateClassUserInfo(ClassUser classUser) {
		int result = userService.updateClassUser(classUser);
		Verify verify = new Verify();
		if (result > 0) {
			verify.setStatus("true");
		} else {
			verify.setStatus("false");
		}
		return verify;
	}

	@ResponseBody
	@RequestMapping("/updatePassword")
	public Verify updatePassword(User user) {
		int result = userService.updatePassword(user);
		Verify verify = new Verify();
		if (result > 0) {
			verify.setStatus("true");
		} else {
			verify.setStatus("false");
		}
		return verify;
	}

	@ResponseBody
	@RequestMapping("/pickRegister")
	public Verify pickRegister(PickCard pickCard) {
		int result = userService.registCard(pickCard);
		Verify verify = new Verify();
		if (result > 0) {
			verify.setStatus("true");
		} else {
			verify.setStatus("false");
		}
		return verify;
	}

	@ResponseBody
	@RequestMapping("/lostCard")
	public Verify lostCard(@RequestParam("user_account") String userAccount) {
		int result = userService.lostCard(userAccount);
		Verify verify = new Verify();
		if (result > 0) {
			verify.setStatus("true");
		} else {
			verify.setStatus("false");
		}
		return verify;
	}

	@ResponseBody
	@RequestMapping("/addFeedbackInfo")
	public Verify addFeedbackInfo(FeedBack feedBack) {
		int result = userService.addFeedbackInfo(feedBack);
		Verify verify = new Verify();
		if (result > 0) {
			verify.setStatus("true");
		} else {
			verify.setStatus("false");
		}
		return verify;
	}

	@ResponseBody
	@RequestMapping("/addPlaceChangeInfo")
	public Verify addPlaceChangeInfo(ChangePlace changePlace) {
		int result = userService.addPlaceChange(changePlace);
		Verify verify = new Verify();
		if (result > 0) {
			verify.setStatus("true");
		} else {
			verify.setStatus("false");
		}
		return verify;
	}

	@ResponseBody
	@RequestMapping("/addCheckIn")
	public Verify addCheckIn(CheckInInfo checkInInfo) {
		int result = userService.addCheckIn(checkInInfo);
		Verify verify = new Verify();
		if (result > 0) {
			verify.setStatus("true");
		} else {
			verify.setStatus("false");
		}
		return verify;
	}

	@ResponseBody
	@RequestMapping("/addCheckOut")
	public Verify addCheckOut(CheckOutInfo checkOutInfo) {
		int result = userService.addCheckOut(checkOutInfo);
		Verify verify = new Verify();
		if (result > 0) {
			verify.setStatus("true");
		} else {
			verify.setStatus("false");
		}
		return verify;
	}

	@ResponseBody
	@RequestMapping("/addDetentionInfo")
	public Verify addDetentionInfo(DetentionInfo detentionInfo) {
		int result = userService.addDetentionInfo(detentionInfo);
		Verify verify = new Verify();
		if (result > 0) {
			verify.setStatus("true");
		} else {
			verify.setStatus("false");
		}
		return verify;
	}

	@ResponseBody
	@RequestMapping("/cardCost")
	public List<CardCost> cardCost(@RequestParam("user_account") String userAccount, @RequestParam("cost_date") String costDate) {
		return userService.cardCost(userAccount, costDate);
	}

	@ResponseBody
	@RequestMapping("/getLostCard")
	public List<PickCard> getLostCard() {
		return userService.getLostCard();
	}

	@ResponseBody
	@RequestMapping("/getVideoInfoById")
	public VideoInfo getVideoInfo(@RequestParam("id") int id) {
		return userService.getVideoInfo(id);
	}

	@ResponseBody
	@RequestMapping("/getVideoInfoByType")
	public List<VideoInfo> getVideoInfoByType(@RequestParam("class_type") String classType) {
		return userService.getGoodVideoInfoByType(classType);
	}

	@ResponseBody
	@RequestMapping("/getUserCollection")
	public List<UserCollection> getUserCollection(@RequestParam("user_account") String userAccount) {
		return userService.getCollectionInfo(userAccount);
	}

	@ResponseBody
	@RequestMapping("/addUserCollection")
	public Verify addUserCollection(UserCollection collection) {
		int result = userService.addCollectionInfo(collection);
		Verify verify = new Verify();
		if (result > 0) {
			verify.setStatus("true");
		} else {
			verify.setStatus("false");
		}
		return verify;
	}

	@ResponseBody
	@RequestMapping("/isCollection")
	public Verify isCollection(UserCollection collection) {
		Verify ver = new Verify();
		UserCollection u = userService.isCollection(collection);
		if (u != null) {
			ver.setStatus("true");
		} else {
			ver.setStatus("false");
		}
		return ver;
	}

	@ResponseBody
	@RequestMapping("/cancelCollection")
	public Verify cancelCollection(UserCollection collection) {
		int result = userService.cancelCollectionInfo(collection);
		Verify verify = new Verify();
		if (result > 0) {
			verify.setStatus("true");
		} else {
			verify.setStatus("false");
		}
		return verify;
	}

	@ResponseBody
	@RequestMapping("/getCollectionVideo")
	public List<VideoInfo> getCollectionVideo(@RequestParam("user_account") String userAccount) {
		return userService.getCollectionVideo(userAccount);
	}

	@ResponseBody
	@RequestMapping("/getOrderInfo")
	public List<OderInfo> getOrderInfo(@RequestParam("user_account") String userAccount) {
		return userService.getOrderInfo(userAccount);
	}

    @ResponseBody
    @RequestMapping("/getStudentInfo")
    public StuInfo getStudentInfo(@RequestParam("stu_account") String userAccount) {
        return userService.getStuInfo(userAccount);
    }

	@ResponseBody
	@RequestMapping("/getDorScoreInfo")
	public List<DorScore> getDorScoreInfo(@RequestParam("user_account") String userAccount) {
		return userService.getScoreInfoByAccount(userAccount);
	}

	@ResponseBody
	@RequestMapping("/getCartInfo")
	public List<VideoInfo> getCartInfo(@RequestParam("user_account") String userAccount) {
		return userService.getCartVideo(userAccount);
	}

	@ResponseBody
	@RequestMapping("/isInCart")
	public Verify isInCart(UserCart userCart) {
		Verify ver = new Verify();
		UserCart u = userService.isInCart(userCart);
		if (u != null) {
			ver.setStatus("true");
		} else {
			ver.setStatus("false");
		}
		return ver;
	}

	@ResponseBody
	@RequestMapping("/addCartInfo")
	public Verify addCartInfo(UserCart userCart) {
		int result = userService.addCartInfo(userCart);
		Verify verify = new Verify();
		if (result > 0) {
			verify.setStatus("true");
		} else {
			verify.setStatus("false");
		}
		return verify;
	}

	@ResponseBody
	@RequestMapping("/addClassFeedBack")
	public Verify addClassFeedBack(ClassFeedBack classFeedBack) {
		int result = userService.addClassFeedBack(classFeedBack);
		Verify verify = new Verify();
		if (result > 0) {
			verify.setStatus("true");
		} else {
			verify.setStatus("false");
		}
		return verify;
	}

    /**
     * 红木家具App后台代码
     * @return API接口
     */

	@ResponseBody
	@RequestMapping("/getKnowInfo")
	public List<KnowInfo> getKnowInfo(){
		return userService.getKnowInfo();
	}

	@ResponseBody
	@RequestMapping("/getRedWoodInfo")
	public List<RedWood> getRedWoodInfo() {
		return userService.getRedWoodInfo();
	}

	@ResponseBody
	@RequestMapping("/getBrandInfo")
	public List<BrandInfo> getBrandInfo() {
		return userService.getBrandInfo();
	}

	@ResponseBody
	@RequestMapping("/getFurniture")
	public List<FurnitureItem> getFurniture() {
		return userService.getFurniture();
	}

    @ResponseBody
    @RequestMapping("/isInCollection")
    public Verify isInCollection(FurnitureCollection collection) {
        Verify ver = new Verify();
        FurnitureCollection furnitureCollection = userService.isInCollection(collection);
        if (furnitureCollection != null) {
            ver.setStatus("true");
        } else {
            ver.setStatus("false");
        }
        return ver;
    }

    @ResponseBody
    @RequestMapping("/getFurnitureCollectionInfo")
    public List<FurnitureCollection> getFurnitureCollectionInfo(@RequestParam("user_account") String userAccount) {
        return userService.getFurnitureCollectionInfo(userAccount);
    }

    @ResponseBody
    @RequestMapping("/addFurnitureCollectionInfo")
    public Verify addFurnitureCollectionInfo(FurnitureCollection collection) {
        int result = userService.addFurnitureCollectionInfo(collection);
        Verify verify = new Verify();
        if (result > 0) {
            verify.setStatus("true");
        } else {
            verify.setStatus("false");
        }
        return verify;
    }

    @ResponseBody
    @RequestMapping("/cancelFurnitureCollectionInfo")
    public Verify cancelFurnitureCollectionInfo(FurnitureCollection collection) {
        int result = userService.cancelFurnitureCollectionInfo(collection);
        Verify verify = new Verify();
        if (result > 0) {
            verify.setStatus("true");
        } else {
            verify.setStatus("false");
        }
        return verify;
    }

    @ResponseBody
    @RequestMapping("/login")
    public Verify login(UserVerify userVerify) {
        Verify ver = new Verify();
        UserVerify u = userService.getUserVerify(userVerify);
        if (u != null) {
            ver.setStatus("true");
        } else {
            ver.setStatus("false");
        }
        return ver;
    }

    @ResponseBody
    @RequestMapping("/furnitureRegister")
    public Verify furnitureRegister(FurnitureUser user) {
        int result = userService.furnitureRegister(user);
        Verify verify = new Verify();
        if (result > 0) {
            verify.setStatus("true");
        } else {
            verify.setStatus("false");
        }
        return verify;
    }

    @ResponseBody
    @RequestMapping("/getUserInfoByName")
    public FurnitureUser getUserInfoByName(@RequestParam("user_account") String userAccount) {
	    return userService.getUserInfoByName(userAccount);
    }

    @ResponseBody
    @RequestMapping("/updateUserInfo")
    public Verify updateUserInfo(FurnitureUser user) {
        int result = userService.updateUserInfo(user);
        Verify verify = new Verify();
        if (result > 0) {
            verify.setStatus("true");
        } else {
            verify.setStatus("false");
        }
        return verify;
    }

    @ResponseBody
    @RequestMapping("/addOrderInfo")
    public Verify addOrderInfo(FurnitureOrder order) {
        int result = userService.addOrderInfo(order);
        Verify verify = new Verify();
        if (result > 0) {
            verify.setStatus("true");
        } else {
            verify.setStatus("false");
        }
        return verify;
    }
}